package org.linlinjava.litemall.db.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.github.yulichang.base.MPJBaseServiceImpl;
import org.linlinjava.litemall.db.dao.LitemallCouponUserMapper;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallCategoryExample;
import org.linlinjava.litemall.db.domain.LitemallCouponUser;
import org.linlinjava.litemall.db.domain.LitemallCouponUserExample;
import org.linlinjava.litemall.db.util.CouponUserConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallCouponUserService extends MPJBaseServiceImpl<LitemallCouponUserMapper,LitemallCouponUser> {

//    private LitemallCouponUser.Column[] CHANNEL = {LitemallCouponUser.Column.id, LitemallCategory.Column.name, LitemallCategory.Column.iconUrl};


    @Resource
    private LitemallCouponUserMapper couponUserMapper;

    public Integer countCoupon(Integer couponId) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andCouponIdEqualTo(couponId).andDeletedEqualTo(false);
        return (int)couponUserMapper.countByExample(example);
    }

    public Integer countCoupon(Integer couponId, Short status) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andCouponIdEqualTo(couponId).andStatusEqualTo(status).andDeletedEqualTo(false);
        return (int)couponUserMapper.countByExample(example);
    }

    public Integer countUserAndCoupon(Integer userId, Integer couponId) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andUserIdEqualTo(userId).andCouponIdEqualTo(couponId).andDeletedEqualTo(false);
        return (int)couponUserMapper.countByExample(example);
    }

    public void add(LitemallCouponUser couponUser) {
        couponUser.setAddTime(LocalDateTime.now());
        couponUser.setUpdateTime(LocalDateTime.now());
        couponUserMapper.insertSelective(couponUser);
    }

    public List<LitemallCouponUser> queryList(Integer userId, Integer couponId, Short status, Integer page, Integer size, String sort, String order) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if(couponId != null){
            criteria.andCouponIdEqualTo(couponId);
        }


        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        if (!StringUtils.isEmpty(page) && !StringUtils.isEmpty(size)) {
            //PageHelper.startPage(page, size);
        }

        return couponUserMapper.selectByExample(example);
    }

//    public List<LitemallCouponUser> queryByOrderIds(List<Integer> orderIds) {
//        LitemallCouponUserExample example = new LitemallCouponUserExample();
//        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
//
//        if
//
//        return couponUserMapper.selectByExample(example);
//    }




    public List<LitemallCouponUser> queryAll(Integer userId, Integer couponId) {
        return queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
    }

    public List<LitemallCouponUser> queryAll(Integer userId) {
        return queryList(userId, null, null, null, null, "add_time", "desc");
    }

    public List<LitemallCouponUser> queryAllByStatus(Integer userId) {
        return queryList(userId, null, CouponUserConstant.STATUS_OUT, null, null, "add_time", "desc");
    }

    public LitemallCouponUser queryOne(Integer userId, Integer couponId) {
        List<LitemallCouponUser> couponUserList = queryList(userId, couponId, null, 1, 1, "add_time", "desc");
        if(couponUserList.size() == 0){
            return null;
        }
        return couponUserList.get(0);
    }

    public LitemallCouponUser queryOneByStatus(Integer userId, Integer couponId) {
        List<LitemallCouponUser> couponUserList = queryList(userId, couponId, CouponUserConstant.STATUS_OUT, 1, 1, "add_time", "desc");
        if(couponUserList.size() == 0){
            return null;
        }
        return couponUserList.get(0);
    }

    public LitemallCouponUser findById(Integer id) {
        return couponUserMapper.selectByPrimaryKey(id);
    }


    public int update(LitemallCouponUser couponUser) {
        couponUser.setUpdateTime(LocalDateTime.now());
        return couponUserMapper.updateByPrimaryKeySelective(couponUser);
    }

    public List<LitemallCouponUser> queryExpired() {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andStatusEqualTo(CouponUserConstant.STATUS_USABLE).andEndTimeLessThan(LocalDateTime.now()).andDeletedEqualTo(false);
        return couponUserMapper.selectByExample(example);
    }

    public List<LitemallCouponUser> queryByCoupon(Integer couponId, Short status) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
        if(status != null){
            criteria.andStatusEqualTo(status);
        }

        if(couponId != null){
            criteria.andCouponIdEqualTo(couponId).andDeletedEqualTo(false);
        }else{
            criteria.andDeletedEqualTo(false);
        }

        criteria.andStatusEqualTo(CouponUserConstant.STATUS_USED);

        return couponUserMapper.selectByExample(example);
    }

    public List<LitemallCouponUser> queryByOrderId(Integer orderId) {
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andOrderIdEqualTo(orderId).andStatusEqualTo(CouponUserConstant.STATUS_USED).andDeletedEqualTo(false);
        return couponUserMapper.selectByExample(example);
    }

    public int recover(LitemallCouponUser couponUser) {
        couponUser.setUpdateTime(LocalDateTime.now());
        return couponUserMapper.updateByPrimaryKey(couponUser);
    }

    public List<LitemallCouponUser> queryAllByDist(LocalDateTime startTime,LocalDateTime endTime,Integer shopId){
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
        if(startTime != null){
            criteria.andUsedTimeGreaterThan(startTime);
        }

        if(endTime != null){
            criteria.andUsedTimeLessThan(endTime);
        }

        criteria.andDeletedEqualTo(false);

        if(shopId != null){
            criteria.andShopIdEqualTo(shopId);
        }


        return couponUserMapper.selectByExample(example);
    }

    public int queryByCode(LitemallCouponUser couponUser) {
        couponUser.setUpdateTime(LocalDateTime.now());
        return couponUserMapper.updateByPrimaryKey(couponUser);
    }

    public Integer queryByUserId(Integer userId){
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        example.or().andUserIdEqualTo(userId).andStatusEqualTo((short)0).andDeletedEqualTo(false);
        return (int) couponUserMapper.countByExample(example);
    }

    public Integer queryByLimit(Integer userId,Integer couponId,LocalDateTime startTime,LocalDateTime endTime){
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andCouponIdEqualTo(couponId).andStatusEqualTo(CouponUserConstant.STATUS_USED).andDeletedEqualTo(false);

        if(startTime != null && endTime != null){
            criteria.andUsedTimeBetween(startTime,endTime);
        }
        return (int) couponUserMapper.countByExample(example);
    }

    public Integer queryByLimitInUse(Integer userId,Integer couponId,LocalDateTime startTime,LocalDateTime endTime){
        LitemallCouponUserExample example = new LitemallCouponUserExample();
        LitemallCouponUserExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andStatusEqualTo((short)1).andCouponIdEqualTo(couponId).andDeletedEqualTo(false);

        if(startTime != null && endTime != null){
            criteria.andUsedTimeBetween(startTime,endTime);
        }
        return (int) couponUserMapper.countByExample(example);
    }
}
